package br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.user;

import br.edu.ufcg.lsd.wbis.ui.client.beans.UserInfo;
import br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.allusers.AllUsersTab;
import br.edu.ufcg.lsd.wbis.ui.client.ui.management.InfoManager;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.widgets.Component;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.TabPanel;

/**
 * @author Renato Miceli
 */
public class RemoveUserCallback implements AsyncCallback<Void> {

	private final TabPanel centerPanel;
	private InfoManager<UserInfo> infoManager;

	public RemoveUserCallback(TabPanel centerPanel, InfoManager<UserInfo> infoManager) {
		this.centerPanel = centerPanel;
		this.infoManager = infoManager;
	}

	/**
	 * {@inheritDoc}
	 */
	public void onFailure(Throwable caught) {
		MessageBox.alert("Error", "User could not be removed!");
	}

	/**
	 * {@inheritDoc}
	 */
	public void onSuccess(Void result) {
	
		MessageBox.alert("Success", "User removed successfully!");
		String tabId = UserTab.getTabId(infoManager.getInfo().getLogin());
		for (Component c : this.centerPanel.getItems()) {
			if(c.getItemId().equals(tabId)){
				centerPanel.remove(c, true);
			} else if(c.getItemId().equals(AllUsersTab.ITEM_ID)){
				AllUsersTab allUsers = (AllUsersTab)c;
				allUsers.update();
			}
		}
		
		
	}

}
